//
//  ShopLocalViewController.m
//  Findif
//
//  Created by Cicel Chen on 2018/10/26.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "ShopLocalViewController.h"
//地图
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
#import <MapKit/MapKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
#import "MapLocalTableViewCell.h"

@interface ShopLocalViewController ()<MAMapViewDelegate, AMapLocationManagerDelegate,UITableViewDelegate,UITableViewDataSource,AMapSearchDelegate>
//AMapSearchDelegate
@property (nonatomic, strong)MAMapView * mapView;
@property (nonatomic, strong)AMapLocationManager *locationManager;
@property (nonatomic, strong)CLLocation * currentLocation;
@property (nonatomic, strong)AMapSearchAPI * search;
@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray * localModelArray;

@end

@implementation ShopLocalViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = ZRBlackColor;
    
    [self setupNav];
    [self setupUI];
}

- (void)returnLocalName:(SelectedLocalBlock)block{
    
    self.selectedLocalBlock = block;
}

- (void)setupNav{
    
    self.title = @"商铺地址";
    
//    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back(1)"] style:UIBarButtonItemStylePlain target:nil action:nil];
//    
//    self.navigationItem.backBarButtonItem = backItem;
}


- (void)setupUI{

    ///初始化地图
    self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, kStatusBarHeight + kNavHeight, kScreenW, 300 * SCREENH6)];

    self.mapView.showsUserLocation = YES;
    self.currentLocation = self.mapView.userLocation.location;
    self.mapView.delegate = self;
    self.mapView.showsCompass = NO;
    self.mapView.showsScale = NO;
    [self.mapView setZoomLevel:14 animated:YES];
    self.mapView.userTrackingMode = MAUserTrackingModeFollow;

    ///把地图添加至view
    [self.view addSubview:_mapView];

    self.search = [AMapSearchAPI new];
    self.search.delegate = self;


    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kStatusBarHeight + kNavHeight + 300 * SCREENH6, kScreenW, kScreenH - 300 * SCREENH6 - kStatusBarHeight - kNavHeight) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ZRWhiteColor;
    tableView.rowHeight = 60 * SCREENH6;
//    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    self.tableView = tableView;

    [self.tableView registerClass:[MapLocalTableViewCell class] forCellReuseIdentifier:@"MapLocalTableViewCell"];

    [self.view addSubview:self.tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
//    NSLog(@"aaa===%lu",(unsigned long)self.localModelArray.count);
    return self.localModelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MapLocalTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MapLocalTableViewCell" forIndexPath:indexPath];
    
    AMapPOI * poi = self.localModelArray[indexPath.row];
    
    cell.name = poi.name;
    cell.addrss = poi.address;
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MapLocalTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    if (self.selectedLocalBlock != nil) {
        
        AMapPOI * poi = self.localModelArray[indexPath.row];
        AMapGeoPoint *location = poi.location;
        NSString * longitude = [NSString stringWithFormat:@"%f",location.longitude];
        NSString * latitude = [NSString stringWithFormat:@"%f",location.latitude];
        
        NSDictionary * dict = @{@"longitude":longitude,@"latitude":latitude,@"name":cell.name};
        
        self.selectedLocalBlock(dict);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)initSearch{
    
//    if (self.currentLocation == nil || self.search == nil) {
//
//        return;
//    }
    
    AMapPOIAroundSearchRequest * request = [AMapPOIAroundSearchRequest new];
    
    request.location = [AMapGeoPoint locationWithLatitude:self.currentLocation.coordinate.latitude longitude:self.currentLocation.coordinate.longitude];
    
//    switch (self.selectedTag) {
//        case 1:
//            request.types = @"写字楼";
//            break;
//        case 2:
//            request.types = @"小区";
//            break;
//        case 3:
//            request.types = @"学校";
//            break;
//        default:
//            request.types = @"汽车服务";
//            break;
//    }
    request.types = @"写字楼|小区|学校|商务住宅";
    request.sortrule = 0;
    request.requireSubPOIs = YES;
    request.radius = 1000;
    
    [self.search AMapPOIAroundSearch:request];
}

- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response{
//
//
//    for (AMapPOI * poi in response.pois) {
//
//        NSLog(@"==%@",poi.name);
//        NSLog(@"==%@",poi.address);
//
//    }
    if ((response.pois.count == 0)) {

        return;
    }
    
    self.localModelArray = response.pois;
    
    [self.tableView reloadData];

//    NSLog(@"res====%@",response.pois);
}

- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
    
    if (self.currentLocation == nil) {
        
        self.currentLocation = userLocation.location;
        
         [self initSearch];
    }
    
    
    
}

- (void)mapViewDidStopLocatingUser:(MAMapView *)mapView{
    
   
    
}


- (void)localUserLocal{
    
    self.mapView.centerCoordinate = self.mapView.userLocation.coordinate;
}


@end
